﻿#region Disclaimer/Info
///////////////////////////////////////////////////////////////////////////////////////////////////
// MetaCollection
// 
// MetaCollection is a system to provide a standard structure, permissions and metadata service
// for content organization.
//
// This project is hosted at Google Code at http://code.google.com/p/metacollection/
//
// This project is licensed under the BSD license.  See the License.txt file for more information.
///////////////////////////////////////////////////////////////////////////////////////////////////
#endregion

using System;
using System.Security.Principal;
using System.Web;
using MetaCollection.Framework.Model;
using MetaCollection.Framework.Security;

namespace MetaCollection.ContentService.WebDav
{
	/// <summary>
	/// Wraps an HttpContext and creates Dav Request and Response instances that map
	/// to the associated Http data.
	/// </summary>
	public class DavContext
	{
		/// <summary>
		/// Internal reference to the HttpContext this web dav context is wrapping
		/// </summary>
		private HttpContext m_HttpContext;

		/// <summary>
		/// Initializes a new instance of the <see cref="DavContext"/> class.
		/// </summary>
		/// <param name="context">The HTTP Context to encapsulate into a WebDav Context.</param>
		public DavContext(HttpContext context)
		{
			if (context == null)
				throw new ArgumentNullException("context");

			m_HttpContext = context;
			Request = new DavRequest(context.Request, this);
			Response = new DavResponse(context.Response, this);
			VerifyMethodEnabled(Request.Method);
		}

		/// <summary>
		/// Verifies the method is enabled
		/// </summary>
		/// <param name="method">The Http method.</param>
		private void VerifyMethodEnabled(Http.HttpMethods method)
		{
			if ((method & SupportedMethods) != method)
				throw new Http.ClientException(Http.ClientErrorStatus.MethodNotAllowed);
		}

		/// <summary>
		/// The Dav Request associated with this Context.
		/// </summary>
		public DavRequest Request { get; private set; }
		/// <summary>
		/// The Dav Response associated with this Context.
		/// </summary>
		public DavResponse Response { get; private set; }
		/// <summary>
		/// The Item <see cref="MetaCollection.Framework.Model.IItemInstance"/> associated with this context.
		/// </summary>
		public IItemInstance Item { get { return m_HttpContext.Items["RequestItem"] as IItemInstance; } }
		/// <summary>
		/// The User associated with this Context that is used for security permissions processing
		/// </summary>
		/// <remarks>Our HTTP module installs this MetaPrincipal for the current request context</remarks>
		public MetaPrincipal User { get { return (MetaPrincipal)m_HttpContext.User; } }
		/// <summary>
		/// The WebDav (Http) methods supported in this context.
		/// </summary>
		public Http.HttpMethods SupportedMethods { get { return (Http.HttpMethods)m_HttpContext.Items["SupportedMethods"]; } }
	}
}
